﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Caching;
using System.Data;
using System.Xml.Serialization;
using Common;
using MvcApplication.Dal;
using MvcApplication.Models;
using System.Xml;
using System.Reflection;
using System.Xml.Linq;
using System.IO;

namespace MvcApplication.Controllers
{
    public class StudentController:Controller
    {
        public ActionResult RptStudent()
        {
            DataSet ds = DbHelperSQL.Query(@"select top 10 * from student");
            if (ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                //DataRow[] DrMan = GetDataRow(dt, "name is null and sex=1");
            }
            //UpdateModel();
            
            
            return View();
        }

        
        public ActionResult XmlData()
        {
            DataTable dt = StudentDal.GetStudent("select top 10 * from student");
            List<studentModel> lst = new List<studentModel>();
            int iSid=0;
            foreach (DataRow dr in dt.Rows)
            {
                studentModel mod = new studentModel();
                int.TryParse(dr["s#"].ToString(), out iSid);
                mod.SID = iSid;
                lst.Add(mod);
            }
            //将数据放置缓存
            Cache cData = HttpRuntime.Cache;

            if (cData["cData"] == null)
            {
                cData.Insert("cData", lst);
            }

            string xml = LinqXml(lst);
            //string xml = Serialize(lst);
            //XmlSerializer xml = new XmlSerializer(typeof(lst));
            //ViewBag.xml = xml;
            //ViewData["xml"] = xml;
            //Response.ContentType = "text/xml";
            //Response.Charset = "GB2312";
            return View();
        }

        //linq 创建xml
        public static string LinqXml(List<studentModel> lst)
        {
            var data = lst.ToArray();
            XElement x = new XElement("Item",
                from c in data
                select new XElement("stu",
                new XAttribute("ID",c.SID),
                new XElement("Name",c.SNAme)
                )
            );
            return x.ToString();
        }

        //xml序列化集合
        public static string Serialize(List<studentModel> obj)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<Root></Root>");

            foreach (studentModel stu in obj)
            {
                XmlElement item = doc.CreateElement("Item");
                PropertyInfo[] p = stu.GetType().GetProperties();
                foreach (PropertyInfo proInfo in p)
                {
                    if (proInfo.GetValue(stu, null) != null)
                    {
                        XmlElement child = doc.CreateElement(proInfo.Name);
                        child.SetAttribute("Type", proInfo.PropertyType.Name);
                        child.InnerText = proInfo.GetValue(stu, null).ToString();
                        item.AppendChild(child);
                    }
                }
                doc.DocumentElement.AppendChild(item);
            }
            return doc.InnerXml;
        }


        public DataRow[] GetDataRow(DataTable dt,string Conditions)
        {
            if(dt.Rows.Count>0)
            {
                return dt.Select(Conditions);
                
            }
            return null;
        }


    }
}